<?php

namespace app\admin\controller\bottle;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Random extends Backend
{
    
    /**
     * BottleRandomNum模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('BottleRandomNum');

    }
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where=array();
           // $where['is_used']=1;
            $inputs=input();
            $input = json_decode($inputs['filter'], true);
            if(!empty($input['id'])){
                $where['id']=$input['id'];
            }
            if(!empty($input['random_id'])){
                $where['random_id']=array('like','%'.$input['random_id'].'%');
            }
            if(!empty($input['is_used'])){
                $where['is_used']=$input['is_used'];
            }
            if(!empty($input['create_time'])) {
                $date = explode(',', $input['create_time']);
                foreach($date as &$v){
                    $v = strtotime($v);
                }
                $where['create_time'] = array('between', $date);
            }
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order('is_used Desc,id desc')
                ->limit($offset, $limit)
                ->select();
            $data=array('1'=>'未投放','2'=>'已投放','3'=>'未使用','4'=>'已使用');
            foreach ($list as &$l){
                $l['is_used']=$data[$l['is_used']];
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $btns = array('refresh');

        $this->view->assign('btns', $btns);
        return $this->view->fetch();
    }
    public function update($used=''){
        $input=input();
        if(count($input['id'])>100){
            return json(array('code'=>-200,'msg'=>'一次修改最多200条'));
        }
        foreach($input['id'] as $i){
            $random=$this->model->where('id='.$i)->find();

            if(!empty($used)){
                if($used!=$random['is_used']){
                    return json(array('code'=>-200,'msg'=>'ID为'.$i.',状态错误'));
                }
            }

            if($random['is_used']==1 || $random['is_used']==2){
                $used=$random['is_used'];
                $re=$random->setInc('is_used');
                if(!$re){
                    return json(array('code'=>-200,'msg'=>'ID为'.$i.',更改失败'));
                }
            }else{
                return json(array('code'=>-200,'msg'=>'ID为'.$i.',状态错误'));
            }

        }
        return json(array('code'=>200,'msg'=>'成功'));
    }
    public function testAddRandomNum(){
       set_time_limit(0);
//        echo '================ok';
//        $Rt = M('Random_test','t_','DB_CONFIG2');
        //随机生成N个16位的数据并存储至数据库

        $n = 100;
        for($i=0;$i<$n;){
            //查看当前随机数是否存在
            $curRandomNum = $this->randStr();
            $info = $this->model->where(array('random_id'=>$curRandomNum))->find();

            //var_dump($info);die;
            if(empty($info)){
                $addData = array(
                    'random_id'=>$curRandomNum,
                    'create_time'=>time(),
                    'is_used'=>1,
                );
                $addRes = Db::name('BottleRandomNum')->insert($addData);

                if($addRes>0){
                    $i++;
                }
            }else{
                continue;
            }
        }

        return json(array('code'=>200,'msg'=>'生成成功'));

//        $res = $Rt->add($addData);
//        var_dump($res);
//        var_dump($Rt->select());
//        echo date('Y').date('m').date('d').date('H').date('i').date('s');
//        echo $this->randStr();
    }

    /**
     * 随机生成16位数字
     * @param int $m
     * @return string
     */
    private function randStr($m = 16) {
        //当前年月日时分秒(共10)
        //$currTimeStr = date('Y').date('m').date('d').date('H').date('i').date('s');

        $str= '0123456789';
        $new_str= $str[mt_rand(1, 9)];
        for($i= 1; $i<= $m-1; ++$i) {
            $new_str.=$str[mt_rand(0, 9)];
        }
        return $new_str;
    }

}
